package com.crs.service.impl;

import com.crs.context.BaseContext;
import com.crs.dto.ResourcePubDTO;
import com.crs.dto.ResourcesPageQueryDTO;
import com.crs.entity.Resource;
import com.crs.entity.Resource_category;
import com.crs.entity.Resource_image;
import com.crs.exception.BusinessException;
import com.crs.mapper.ResourceImageMapper;
import com.crs.mapper.ResourceMapper;
import com.crs.result.PageResult;
import com.crs.service.ResourceService;
import com.crs.utils.AliOssUtil;
import com.crs.vo.ResourceListVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private ResourceImageMapper resourceImageMapper;
    /**
     * 资源发布
     * @param resourcePubDTO
     */
    @Transactional
    @Override
    public void pub(ResourcePubDTO resourcePubDTO,List<MultipartFile> files) throws IOException {
        //根据传入的dto封装成完整的对象，然后插入数据库
        Resource resource = new Resource();
        BeanUtils.copyProperties(resourcePubDTO,resource);
        resource.setStatus(1);
        resource.setViewCount(0);
        resource.setBorrowCount(0);
        resource.setAllowReservation(1);
        resource.setOwnerId(BaseContext.getCurrentId());

        //将资源插入数据库主键回写，为插入图片获取资源
        resourceMapper.insert(resource);

        List<String> filepaths = new ArrayList<>();

        for (MultipartFile file:files
        ) {
            //判断文件格式必须为图片类型
            if(!file.getContentType().startsWith("image/")){
                throw new BusinessException("上传的文件必须为图片格式");
            }
            //获取每个图片的url
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName = UUID.randomUUID().toString()+extension;
            String filepath = null;
            try {
                filepath = aliOssUtil.upload(file.getBytes(), objectName);
            } catch (IOException e) {
                throw new BusinessException("图片上传失败");
            }
            filepaths.add(filepath);
        }
        //批量插入数据库中Resource_image
        resourceImageMapper.insertBatch(filepaths);
    }

    /**
     * 查找资源列表
     * @return
     */
    @Override
    public PageResult list(ResourcesPageQueryDTO resourcesPageQueryDTO) {
        PageHelper.startPage(resourcesPageQueryDTO.getPage(), resourcesPageQueryDTO.getPageSize());

        Long total = resourceMapper.getTotalCount(resourcesPageQueryDTO);

        List<ResourceListVo> list = resourceMapper.list(resourcesPageQueryDTO);

        PageResult pageResult = new PageResult(total,list);

        return pageResult;
    }

    /**
     * 查看资源详情
     * @param
     * @return
     */
    public ResourceListVo detail(Integer id)  {
        return resourceMapper.detail(id);
    }
}
